All files / util obj.ts

85% Statements 34/40
50% Branches 11/22
77.78% Functions 7/9
85% Lines 34/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                                2x             2x 7817x     2x       2x 1198x 1198x         1198x       2x 435x     2x       5049x 4095x 4095x 4095x 4095x           2x       13195x 13610x 13610x         2x                     2x 2565x       2565x 1027x 1027x     1538x     2x 2213x       2213x 2213x 2149x 2149x     2213x    
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { assert } from './assert';
 
export interface Dict<V> {
  [stringKey: string]: V;
  [numberKey: number]: V;
}
 
export function contains<V>(obj: Dict<V>, key: string | number): boolean {
  return Object.prototype.hasOwnProperty.call(obj, key);
}
 
export function get<V>(obj: Dict<V>, key: string | number): V | null {
  return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : null;
}
 
export function size<V>(obj: Dict<V>): number {
  let count = 0;
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      count++;
    }
  }
  return count;
}
 
/** Returns the given value if it's defined or the defaultValue otherwise. */
export function defaulted<V>(value: V | undefined, defaultValue: V): V {
  return value !== undefined ? value : defaultValue;
}
 
export function forEachNumber<V>(
  obj: Dict<V>,
  fn: (key: number, val: V) => void
): void {
  for (const key in obj) {
    Eif (Object.prototype.hasOwnProperty.call(obj, key)) {
      const num = Number(key);
      Eif (!isNaN(num)) {
        fn(num, obj[key]);
      }
    }
  }
}
 
export function forEach<V>(
  obj: Dict<V>,
  fn: (key: string, val: V) => void
): void {
  for (const key in obj) {
    Eif (Object.prototype.hasOwnProperty.call(obj, key)) {
      fn(key, obj[key]);
    }
  }
}
 
export function lookupOrInsert<V>(
  obj: Dict<V>,
  key: string | number,
  valFn: () => V
): V {
  if (!contains(obj, key)) {
    obj[key] = valFn();
  }
  return obj[key];
}
 
export function isEmpty<V>(obj: Dict<V>) {
  assert(
    obj != null && typeof obj === 'object',
    'isEmpty() expects object parameter.'
  );
  for (const key in obj) {
    Eif (Object.prototype.hasOwnProperty.call(obj, key)) {
      return false;
    }
  }
  return true;
}
 
export function shallowCopy<V>(obj: Dict<V>): Dict<V> {
  assert(
    obj && typeof obj === 'object',
    'shallowCopy() expects object parameter.'
  );
  const result: Dict<V> = {};
  for (const key in obj) {
    Eif (Object.prototype.hasOwnProperty.call(obj, key)) {
      result[key] = obj[key];
    }
  }
  return result;
}